package comic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.graphics.drawable.Drawable;

/**
 * Abstract Factory Class which generates Comic.Strip objects
 */
public abstract class Comic implements Serializable {

	private static final long serialVersionUID = 807793418526894277L;

	/**
	 * Struct which represents a single comic strip
	 */
	public static class Strip {
		public int id;
		public String title;
		public String text;
		public Drawable image;
	}

	/**
	 * returns the text content of the given URL. returns an empty string in
	 * case of an error.
	 */
	public static String get_website_content(String url)
			throws MalformedURLException, IOException {
		StringBuffer buf = new StringBuffer("");

		URL u = new URL(url);
		URLConnection c = u.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(
				c.getInputStream()));

		String inputLine;

		while ((inputLine = in.readLine()) != null)
			buf.append(inputLine);
		in.close();

		return buf.toString();
	}

	public String display_name;
	public int first_comic;
	public ArrayList<Integer> favorites;
	public int lastread;

	public Comic() {
		this.display_name = get_display_name();
		this.first_comic = get_first_comic_n();
		this.lastread = first_comic;			
	}

	@Override
	public String toString() {
		return this.display_name;
	}

	/**
	 * Returns the display name for the comic.
	 */
	abstract public String get_display_name();

	/**
	 * Normally returns 0 or 1. Can be individual for different comics.
	 */
	abstract public int get_first_comic_n();

	/**
	 * Returns the id of the latest comic.
	 * 
	 * Will throw an MalformedURLException or IOException in case of an error
	 * downloading the page.
	 * 
	 * A return value of -1 means there was an error parsing the value.
	 */
	abstract public int getLatest() throws MalformedURLException, IOException;

	/**
	 * Returns the comic strip with the given id
	 */
	abstract public Strip getStrip(int id) throws MalformedURLException,
			IOException;
}
